﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using  VAdvantage.UIEvent;
using System.Windows.Media.Imaging;

namespace VAdvantage.Editor
{
    public class WEditorPopupMenu : ContextMenu, IEventLitnerer
    {


        private const long serialVersionUID = 8172397145177408454L;
        private const String EVENT_ATTRIBUTE = "EVENT";
        public const String ZOOM_EVENT = "ZOOM";
        public const String REQUERY_EVENT = "REQUERY";
        public const String PREFERENCE_EVENT = "VALUE_PREFERENCE";
        public const String NEW_EVENT = "NEW_RECORD";
        public const String UPDATE_EVENT = "UPDATE_RECORD";

        private bool newEnabled = true;
        private bool updateEnabled = true;
        private bool zoomEnabled = true;
        private bool requeryEnabled = true;
        private bool preferencesEnabled = true;

        private MenuItem zoomItem;
        private MenuItem requeryItem;
        private MenuItem prefItem;
        private MenuItem newItem;
        private MenuItem updateItem;

        private List<IContextMenuListener> menuListeners = new List<IContextMenuListener>();

        private IContextMenuListener _listnerer = null;

        public WEditorPopupMenu(bool zoom, bool requery, bool preferences) :
            this(zoom, requery, preferences, false, false)
        {
        }

        public WEditorPopupMenu(bool zoom, bool requery, bool preferences, bool newRecord)
            : this(zoom, requery, preferences, newRecord, false)
        {
        }

        public WEditorPopupMenu(bool zoom, bool requery, bool preferences, bool newRecord, bool updateRecord)
        {
            this.zoomEnabled = zoom;
            this.requeryEnabled = requery;
            this.preferencesEnabled = preferences;
            this.newEnabled = newRecord;
            this.updateEnabled = updateRecord; // Elaine 2009/02/16 - update record
            Init();
        }

        public bool IsZoomEnabled()
        {
            return zoomEnabled;
        }

        private void Init()
        {
            if (zoomEnabled)
            {
                zoomItem = new MenuItem();
                zoomItem.Name = ZOOM_EVENT;
                //zoomItem.Command  = ZOOM_EVENT;
                zoomItem.Header = "Zoom";
                zoomItem.Icon = GetIcon("Zoom");

                zoomItem.Click += delegate
                {
                    OnEvent(new Event(Events.ON_CLICK, zoomItem));
                };

                this.Items.Add(zoomItem);
            }

            if (requeryEnabled)
            {
                requeryItem = new MenuItem();
                requeryItem.Name = REQUERY_EVENT;
                requeryItem.Header = "ReQuery";
                requeryItem.Icon = GetIcon("Refresh");
                requeryItem.Click += delegate
                {
                    OnEvent(new Event(Events.ON_CLICK, requeryItem));
                };
                this.Items.Add(requeryItem);
            }

            if (preferencesEnabled)
            {
                prefItem = new MenuItem();
                prefItem.Name = PREFERENCE_EVENT;
                prefItem.Header = "Value Preference";
                prefItem.Icon = GetIcon("VPreference");
                prefItem.Click += delegate
                {
                    OnEvent(new Event(Events.ON_CLICK, prefItem));
                };
                this.Items.Add(prefItem);
            }

            if (newEnabled)
            {
                newItem = new MenuItem();
                newItem.Name = NEW_EVENT;
                newItem.Header = "New Record";
                newItem.Icon = GetIcon("New16.png");
                newItem.Click += delegate
                {
                    OnEvent(new Event(Events.ON_CLICK, newItem));
                };
                this.Items.Add(newItem);
            }

            if (updateEnabled)
            {
                updateItem = new MenuItem();
                updateItem.Name = UPDATE_EVENT;
                updateItem.Header = "Update Record";
                updateItem.Icon = GetIcon("InfoBPartner");
                updateItem.Click += delegate
                {
                    OnEvent(new Event(Events.ON_CLICK, updateItem));
                };
                this.Items.Add(updateItem);
            }
        }

        public void AddMenuListener(IContextMenuListener listener)
        {
            if (!menuListeners.Contains(listener))
                menuListeners.Add(listener);
        }

        public void AddContextListener(IContextMenuListener listnerer)
        {
            _listnerer = listnerer;
        }


        public void OnEvent(Event e)
        {
            String evt = (String)e.GetTarget().Name;

            if (evt != null)
            {
                if (_listnerer != null)
                {
                    _listnerer.OnMenu(new ContextMenuEvent(evt));
                }
                else
                {

                    ContextMenuEvent menuEvent = new ContextMenuEvent(evt);

                    IContextMenuListener[] listeners = new IContextMenuListener[0];
                    listeners = menuListeners.ToArray();
                    foreach (IContextMenuListener listener in listeners)
                    {
                        listener.OnMenu(menuEvent);
                    }
                }
            }
        }

        public  object GetIcon(string imageName)
        {
            Image img = new Image();
            img.Source = new BitmapImage(new Uri("images/" + imageName + "16.png", UriKind.Relative));
            return img;
        }
    }
}
